package com.wits.qso.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wits.comet.chat.entity.ChatMember;
import com.wits.comet.chat.service.CometContext;
import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.AjaxResult;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Student;
import com.wits.qso.entity.Vm;
import com.wits.qso.entity.VmUser;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.StudentService;
import com.wits.qso.service.VmService;
import com.wits.qso.service.VmUserService;

@Controller
@RequestMapping(value="/qso/lesson")
public class LessonController extends BaseController{
	
	@Autowired
	private BtableDaoService btableDaoService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private VmUserService vmUserService;
	
	@Autowired
	private VmService vmService;
	/**
	 * 
	 * 学生听课(如果未传递vmId,则根据当前学生所上课程查询老师的vm)
	 * @return
	 */
	@RequestMapping(value="/listen")
	public String listen(Model model,HttpServletRequest req){
		String vmId=StringUtils.trim(req.getParameter("vmId"));
		Vm vm=null;
		if(StringUtils.isNotEmpty(vmId)){
			vm=vmService.getOne(vmId);
		}else{
			String userId=this.getCurrentUserId();
			Student stu=studentService.findByUserId(userId);
			Btable btable=btableDaoService.findByClsIdAndLessonTime(stu.getClsId(),  DateUtil.getNow());
			String teaUserId=btable.getTeaid().getTerUserId();
			
			//从缓存中获得老师用户
			ChatMember member=CometContext.getInstance().getMember(teaUserId);
			if(member!=null && member.isShareDesktop()){
				VmUser vu=vmUserService.findByUserId(teaUserId);
				if(vu!=null){
					vm=vu.getVm();
				}
			}

		}
		model.addAttribute("vm",vm);
		return "qso/lesson/listen";
	}
	
	/**
	 * 
	 * 检查老师是否已共享桌面
	 * @return
	 */
	@RequestMapping(value="/checkTeaDesktop")
	@ResponseBody
	public AjaxResult checkTeaShareDesktop(){
		String userId=this.getCurrentUserId();
		boolean isShare=false;
		try{
			Student stu=studentService.findByUserId(userId);
			Btable btable=btableDaoService.findByClsIdAndLessonTime(stu.getClsId(),  DateUtil.getNow());
			if(btable!=null){
				String teaUserId=btable.getTeaid().getTerUserId();
				//从缓存中获得老师用户
				ChatMember member=CometContext.getInstance().getMember(teaUserId);
				if(member!=null){
					isShare=member.isShareDesktop();
				}
			}
		}catch(Exception e){
			logger.error("error:",e);
			return AjaxResult.error();
		}
		return AjaxResult.successResult(isShare);
	
	}

	/**
	 * 学生实训
	 * @return
	 */
	@RequestMapping(value="/train")
	public String train(Model model){
		String userId=this.getCurrentUserId();
		try{
			VmUser vu=vmUserService.findByUserId(userId);
			model.addAttribute("vm",vu.getVm());
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return "qso/lesson/train";
	}
}
